package com.cn.wanxi.controller;

import com.alibaba.fastjson.JSONObject;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;

@Controller
public class FileController {
    @RequestMapping("/file")
    @ResponseBody
    public String file(@RequestParam("file") MultipartFile multipartFile, @RequestParam("loginName") String loginName, HttpServletRequest req) throws IOException {
        System.out.println(loginName);

        String username = (String) req.getSession().getAttribute(loginName);
        if (username == null) {
            return "no-login";
        }
        String fileName = System.currentTimeMillis() + "----" + multipartFile.getOriginalFilename();
        String path = "D:\\wanxi\\step-6\\canyin060119\\src\\main\\web\\loadImg";
        multipartFile.transferTo(new File(path, fileName));
//        JSONObject jsonObject = new JSONObject();
//        jsonObject.put("url", "/loadImg/" + fileName);
//        resp.getWriter().println(jsonObject);
        return "/loadImg/" + fileName;
    }
}
